package com.example.touchdemo01.views;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class MultiTouchView extends TextView {
    public static final String TAG = "MultiView";
    private float x1, x2, y1, y2;

    public MultiTouchView(Context context) {
        super(context);
    }

    public MultiTouchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "第1个手指按下");
                x1 = event.getX(0);
                y1 = event.getY(0);
                break;
            case MotionEvent.ACTION_UP:
                Log.e(TAG, "最后1个手指抬起");
                break;
            case MotionEvent.ACTION_MOVE:
                if (event.getPointerCount() >= 2) {
                    x2 = event.getX(1);
                    y2 = event.getY(1);

                    x1 = event.getX(0);
                    y1 = event.getY(0);
                    Log.d(TAG, "======pointer=====move" + "(" + x1 + "," + y1 + "),(" + x2 + "," + y2+")") ;
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.e(TAG,"第"+(index+1)+"个手指按下");
                break;
            case MotionEvent.ACTION_POINTER_UP:
                Log.e(TAG,"第"+(index+1)+"个手指抬起");
                break;
        }
        return true;
    }
}